package cn.stronglink.oil.ipset;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;

import cn.stronglink.oil.R;
import cn.stronglink.oil.exceptions.BusinessErrorException;
import cn.stronglink.oil.utils.HttpClientUtils;
import cn.stronglink.oil.utils.SharedPreferencesUtil;
import cn.stronglink.oil.widget.ExceptionHintDialog;


/**
 * Created by Tgb on 2018/1/17.
 */

public class IpSetActivity extends AppCompatActivity {
    private EditText et_ip;
    private EditText et_camera_ip;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ip_set);
        initView();
    }

    private void initView() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setNavigationIcon(R.drawable.icon_back);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        et_ip = (EditText) findViewById(R.id.et_ip);
        et_ip.setText(SharedPreferencesUtil.getIp(this));
        et_camera_ip = findViewById(R.id.et_camera_ip);
        et_camera_ip.setText(SharedPreferencesUtil.getCameraIp(this));
    }

    public void onSave(View v) {
        String ip = et_ip.getText().toString();
        String cameraIp = et_camera_ip.getText().toString();
        if (ip.isEmpty()) {
            showExceptionMsg(new BusinessErrorException(getString(R.string.ip_not_null)));
            return;
        }
        if(cameraIp.isEmpty()){
            showExceptionMsg(new BusinessErrorException(getString(R.string.camera_ip_not_null)));
            return;
        }
        SharedPreferencesUtil.saveIp(this, ip);
        HttpClientUtils.setHOST(ip);
        SharedPreferencesUtil.saveCameraIp(this,cameraIp);
        HttpClientUtils.setCameraHost(cameraIp);
        finish();
    }

    /**
     * 显示异常
     *
     * @param e
     */
    private void showExceptionMsg(Exception e) {
        final ExceptionHintDialog dialog = new ExceptionHintDialog(IpSetActivity.this);
        dialog.setDialogMessage(e.getMessage() + "");
        dialog.setOnOkClickListener(new ExceptionHintDialog.OnOkClickListener() {
            @Override
            public void onOkClick() {
                dialog.dismiss();
            }
        });
        dialog.show();
    }
}
